import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login/index.vue') // 路由懒加载
  }, {
    path: '/reg',
    name: 'register',
    component: () => import('../views/Reg/index.vue') // 路由懒加载
  },
  {
    path: '/',
    component: () => import('../views/layout/index.vue'), // 路由懒加载
    children: [{
      path: '', component: () => import('../views/Home/index.vue')
    }, {
      path: 'ArtCategory', component: () => import('../views/art/artcategory.vue')
    }, {
      path: 'ArtList', component: () => import('../views/art/artlist.vue')
    }, {
      path: 'UserInfo', component: () => import('../views/user/userinfo.vue')
    }, {
      path: 'changeAvatar', component: () => import('../views/user/changeavatar.vue')
    }, {
      path: 'resetPwd', component: () => import('../views/user/resetpwd.vue')
    }]
  },
  {
    path: '*',
    component: () => import('../views/NoFound.vue') // 路由懒加载
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // 定义一个白名单
  const whiteList = ['/login', '/res']
  // console.log(whiteList)
  // 判断是否存在白名单里面
  if (whiteList.includes(to.path)) {
    // 放行
    next()
  } else {
    if (localStorage.getItem('token')) {
      next()
    } else {
      next(false)
    }
  }
})

export default router
